﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;

namespace Tesla.Graphics {
    /// <summary>
    /// Provides a general way to bind render targets to the graphics pipeline.
    /// </summary>
    public struct RenderTargetBinding {
        private bool _isTargetCube;
        private Texture _target;
        private CubeMapFace _face;

        /// <summary>
        /// Gets if the render target is a cube target.
        /// </summary>
        public bool IsRenderTargetCube {
            get {
                return _isTargetCube;
            }
        }

        /// <summary>
        /// Gets the render target as a 2D target.
        /// </summary>
        public RenderTarget2D RenderTarget2D {
            get {
                return _target as RenderTarget2D;
            }
        }

        /// <summary>
        /// Gets the render target as a cube target.
        /// </summary>
        public RenderTargetCube RenderTargetCube {
            get {
                return _target as RenderTargetCube;
            }
        }

        /// <summary>
        /// Gets the face of the cube map to use, if a <see cref="RenderTargetCube"/>.
        /// </summary>
        public CubeMapFace CubeMapFace {
            get {
                return _face;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetBinding"/>.
        /// </summary>
        /// <param name="renderTarget">The render target.</param>
        public RenderTargetBinding(RenderTarget2D renderTarget) {
            if(renderTarget == null) {
                throw new ArgumentNullException("renderTarget", "The render target to be bound cannot be null.");
            }
            _isTargetCube = false;
            _target = renderTarget;
            _face = Graphics.CubeMapFace.PositiveX;
        }

        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetBinding"/>.
        /// </summary>
        /// <param name="renderTarget">The render target cube.</param>
        /// <param name="cubeFace">The cube face.</param>
        public RenderTargetBinding(RenderTargetCube renderTarget, CubeMapFace cubeFace) {
            if(renderTarget == null) {
                throw new ArgumentNullException("renderTarget", "The render target to be bound cannot be null.");
            }
            _isTargetCube = true;
            _target = renderTarget;
            _face = cubeFace;
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="Tesla.Graphics.RenderTarget2D"/> to <see cref="Tesla.Graphics.RenderTargetBinding"/>.
        /// </summary>
        /// <param name="renderTarget">The render target.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator RenderTargetBinding(RenderTarget2D renderTarget) {
            return new RenderTargetBinding(renderTarget);
        }
    }
}
